package com.app.view.component 
{
	import flash.display.GradientType;
	import flash.geom.Matrix;
	import flash.display.Sprite;
	import flash.geom.Point;

	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;	
	
	import com.greensock.TweenLite;
	
	import com.app.view.manager.LayerManagerMediator;
	import com.app.view.manager.SceneManagerMediator;
	import com.app.view.manager.SoundManagerMediator;
	import com.app.view.component.scene.RoomScene;
	
	import com.app.model.RoomConnProxy;
	
	import com.app.model.data.UserData;
	import com.app.model.data.LevelData;
	
	import com.peachcastle.debug.Log;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class RoomMediator extends Mediator implements IMediator
	{
		public static const NAME:String 						= "RoomMediator";
		public static const ADD_IN_LAYER:String 				= NAME + "_AddInLayer";
		
		public static const ADD_PLAYER_IN_SEAT_RESPONSE:String 	= NAME + "_AddPlayerInSeatResponse"; 
		
		public static const INIT_ROOM_REQUEST:String 			= NAME + "_InitRoomRequest";
		public static const SIT_DOWN_REQUEST:String 			= NAME + '_SitDownRequest';
		public static const REAL_SIT_DOWN_REQUEST:String 		= NAME + '_RealSitDownRequest';
		public static const SIT_DOWN_RESPONSE:String 			= NAME + '_SitDownResponse';
		public static const STAND_REQUEST:String 				= NAME + '_StandRequest';
		public static const STAND_RESPONSE:String 				= NAME + '_StandResponse';
		public static const GO_HALL_REQUEST:String 				= NAME + '_GoHallRequest';
		public static const GO_HALL_RESPONSE:String 			= NAME + '_GoHallResponse';
		public static const START_GAME_REQUEST:String 			= NAME + '_StartGameRequest';
		public static const START_GAME_RESPONSE:String 			= NAME + '_StartGameResponse';
		
		public static const BASE_POUR_REQUEST:String 			= NAME + '_BasePourRequest';
		public static const BASE_POUR_RESPONSE:String 			= NAME + '_BasePourResponse';
		public static const CALL_POUR_START_RESPONSE:String 	= NAME + '_CallPourStartResponse';
		public static const POUR_REQUEST:String 				= NAME + '_PourRequest';
		public static const POUR_RESPONSE:String 				= NAME + '_PourResponse';
		public static const START_PK_RESPONSE:String 			= NAME + '_StartPkResponse';
		public static const PK_RESULT_REQUEST:String 			= NAME + '_PkResultRequest';
		public static const PK_RESULT_RESPONSE:String 			= NAME + '_PkResultResponse';
		public static const GIVE_UP_RESPONSE:String 			= NAME + '_GiveupResponse';
		public static const GAME_OVER_RESPONSE:String 			= NAME + '_GameOverResponse';
		
		public static const CHAT_REQUEST:String                 = NAME + "_ChatRequest";
		public static const CHAT_RESPONSE:String                = NAME + "_ChatResponse";
		
		public static const EXPRESSION_REQUEST:String                 = NAME + "_ExpressionRequest";
		public static const EXPRESSION_RESPONSE:String                = NAME + "_ExpressionResponse";
		
		public static const EXP_ADD_RESPONSE:String             = NAME + "_ExpAddResponse";
		
		public static const SHOW_LAST_RESULT_REQUEST:String     = NAME + "_ShowLastResultRequest";

		private var _roomSence:RoomScene = null;		
		
		private var _selfSeatid:int = 0;
		private var _bankerSeatid:int = 1;
		
		public function RoomMediator(roomSence:RoomScene) 
		{
			super(NAME);
			
			_roomSence = roomSence;
			_roomSence.setMediator(this);
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				RoomMediator.ADD_IN_LAYER,
				RoomMediator.ADD_PLAYER_IN_SEAT_RESPONSE,
				RoomMediator.SIT_DOWN_RESPONSE,
				RoomMediator.STAND_RESPONSE,
				RoomMediator.GO_HALL_RESPONSE,
				RoomMediator.START_GAME_RESPONSE,
				RoomMediator.BASE_POUR_RESPONSE,
				RoomMediator.CALL_POUR_START_RESPONSE,
				RoomMediator.POUR_RESPONSE,
				RoomMediator.START_PK_RESPONSE,
				RoomMediator.PK_RESULT_RESPONSE,
				RoomMediator.GIVE_UP_RESPONSE,
				RoomMediator.GAME_OVER_RESPONSE,
				
				RoomMediator.CHAT_RESPONSE,
				RoomMediator.EXPRESSION_RESPONSE,
				
				RoomMediator.EXP_ADD_RESPONSE,
				
				LayerManagerMediator.GET_LAYERS,
				SceneManagerMediator.CURRENT_SCENE
			];
		}	
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var param:* = notification.getBody();
			var user:UserData = null;
			switch ( name )
			{
				case RoomMediator.ADD_IN_LAYER:
					_roomSence.setUserData(param[0], param[1]);
					_roomSence.setRoomData(param[2], param[3], param[4]);
					_roomSence.addLayer();
					sendNotification(SceneManagerMediator.LEAVE_PRE_SCENE);
					sendNotification(LayerManagerMediator.ADD_IN_LAYER, [LayerManagerMediator.ROOM_LAYER, _roomSence.assest]);
					sendNotification(SceneManagerMediator.PRE_LOAD, ["HallScene"]);
					
					break;
				
				case RoomMediator.ADD_PLAYER_IN_SEAT_RESPONSE:
					user = param[0] as UserData;
					_roomSence.playerSeats.addPlayer(user.seatid, user);
					if (user.isOver == false)
					{
						_roomSence.chipsManager.showUserChips(user.seatid, user.totalCallChips);
						if(user.paiIds) _roomSence.playerSeats.setPaisBg(user.seatid, true);
					}
				
					break;
					
				case RoomMediator.SIT_DOWN_RESPONSE:
					user = param[2] as UserData;
					
					//是否是自己坐下
					if (param[0] == true)
					{
						sendNotification(SoundManagerMediator.PLAY, [SoundManagerMediator.SEAT_SIT]);
						if (user.seatid != _bankerSeatid)
						{
							_roomSence.showStand(true);
							_roomSence.toolbar.update(user);
						}
						_roomSence.playerSeats.setSeats(false);
						
						if (user.isOver == false && user.paiIds)
						{
							_roomSence.playerSeats.showPaijiuValues(_selfSeatid, 1, 4, user.paiIds.split(","), true);
							_roomSence.playerSeats.setPaisBg(user.seatid, false);
						}
					}
					
					_selfSeatid = param[1];
					if (_selfSeatid == _bankerSeatid && param[3] > 1 && param[4] == RoomConnProxy.STAT_WAIT_START)	//	庄家
					{
						_roomSence.playerSeats.setOperation(_selfSeatid, 'banker', 'startGame', true);
					}
						
					break;
					
				case RoomMediator.STAND_RESPONSE:
					_roomSence.playerSeats.removePlayer(param[1]);
					_roomSence.playerSeats.stopClock(param[1]);
					_roomSence.chipsManager.showUserChips(param[1], 0);
					if (param[0] == true)
					{
						_roomSence.showStand(false);
						sendNotification(SoundManagerMediator.PLAY, [SoundManagerMediator.SEAT_STAND]);
						_selfSeatid = 0;
						_roomSence.playerSeats.setSeats(true, param[2]);
						
						_roomSence.toolbar.update(param[3] as UserData);
					}

					if ((param[3] as UserData).seatid == _bankerSeatid && param[2].length <= 1)
					{
						_roomSence.playerSeats.setOperation(_selfSeatid, 'banker', 'startGame', false);
					}
					
					break;
					
				case RoomMediator.GO_HALL_RESPONSE:
				
					break;
					
				case RoomMediator.START_GAME_RESPONSE:
					_roomSence.showGameTip('start');

					_roomSence.playerSeats.startClock(param[1], param[2]);
					if (param[0] > _bankerSeatid && param[0] == _selfSeatid) 
					{	//闲家的显示
						_roomSence.playerSeats.setOperation(param[0], 'playr', 'basePour', true, param[3], param[4]);
					}
				
					break;
					
				case RoomMediator.BASE_POUR_RESPONSE:
					_roomSence.playerSeats.stopClock(param[0]);
					
					playCallChipSound(param[1]);

					_roomSence.chipsManager.showUserChips(param[0], param[1]);
					_roomSence.playerSeats.updateSeatInfo(param[0], param[2]);
				
					break;
					
				case RoomMediator.CALL_POUR_START_RESPONSE:
					if (_selfSeatid > _bankerSeatid && param[0] == _selfSeatid)
					{
						_roomSence.playerSeats.setOperation(param[0], 'playr', 'basePour', false, 0, param[7]);
					}
					
					startDeal(param[0], param[2], param[3], param[4], param[5], param[1], param[6]);
					
					break;
					
				case RoomMediator.POUR_RESPONSE:
					_roomSence.playerSeats.changePaijiuStatus(param[0], false);
					_roomSence.playerSeats.stopClock(param[0]);
					if (param[0] > _bankerSeatid && param[0] == _selfSeatid) 
					{	//闲家的显示
						_roomSence.playerSeats.setOperation(param[0], 'playr', 'callPour', false);
					}

					playCallChipSound(param[1]);

					_roomSence.chipsManager.showUserChips(param[0], param[2]);
					_roomSence.playerSeats.updateSeatInfo(param[0], param[3]);
				
					break;
					
				case RoomMediator.START_PK_RESPONSE:
					_roomSence.playerSeats.startClock([_bankerSeatid], param[2], true);
					if (param[0] == _bankerSeatid)
					{	//庄家可以看到闲家的比牌按钮
						_roomSence.playerSeats.setOperation(param[1], 'banker', 'pk', true);
					}
					
					_roomSence.showGameTip('pk', param[3]);
					
					break;
					
				case RoomMediator.PK_RESULT_RESPONSE:
					var banker:UserData = param[1];
					var playr:UserData = param[2];
					
					_roomSence.chipsManager.showUserChips(banker.seatid, 0);	//先清空庄家桌面上的筹码

					for (var k:* in param[3])
					{
						if(k != banker.seatid || (k == banker.seatid && !banker.isPkResult))
							_roomSence.playerSeats.showResult(k, param[3][k].split(","));
					}
					
					
					if (param[0] == 1)
					{//闲家赢
						_roomSence.playerSeats.setGameStatus(playr.seatid, 'win', true);
						_roomSence.chipsManager.showUserChips(banker.seatid, param[4]);
						_roomSence.chipsManager.moveSeatToSeat(banker.seatid, playr.seatid, param[4] + playr.totalCallChips);
					}else if(param[0] == 2)
					{//庄家赢
						_roomSence.playerSeats.setGameStatus(playr.seatid, 'loss', true);
						_roomSence.chipsManager.moveSeatToSeat(playr.seatid, banker.seatid, param[4]);
					}else 
					{
						Log.info("tie game...");
					}
					
					_roomSence.playerSeats.updateSeatInfo(banker.seatid, banker);
					_roomSence.playerSeats.updateSeatInfo(playr.seatid, playr);
					
					if (param[5] == _bankerSeatid && param[6] > _bankerSeatid)
					{//庄家可以看到闲家的比牌按钮
						_roomSence.playerSeats.setOperation(param[6], 'banker', 'pk', true);
					}
					
					if (param[6] > _bankerSeatid)
					{
						TweenLite.delayedCall(.5, _roomSence.showGameTip, ['pk', param[7]]);
					}
					
					break;
				
				case RoomMediator.GIVE_UP_RESPONSE:
					_roomSence.playerSeats.setGameStatus(param[0], 'giveup', true);
					_roomSence.playerSeats.stopClock(param[0]);
					_roomSence.chipsManager.moveSeatToSeat(param[0], 1, param[1]);
				
					if (param[0] != _bankerSeatid && param[0] == _selfSeatid) 
					{//闲家的显示
						_roomSence.playerSeats.setOperation(param[0], 'playr', 'callPour', false);

						_roomSence.playerSeats.resetSeat(param[0]);
					}
					
					if ((param as Array).length >= 4)
					{
						var gBanker:UserData = param[2];
						if(gBanker) _roomSence.playerSeats.updateSeatInfo(_bankerSeatid, gBanker);
						var gUser:UserData = param[3];
						if (gUser && gUser.seatid == _bankerSeatid) _roomSence.toolbar.update(gUser);
					}

					_roomSence.playerSeats.setOperation(param[0], 'banker', 'pk', false);
					
					break;
					
				case RoomMediator.GAME_OVER_RESPONSE:
					user = param[0] as UserData;
					if(user.seatid == _bankerSeatid) _roomSence.toolbar.update(user);
				
					_roomSence.playerSeats.stopAllClock();
					TweenLite.delayedCall(3, gameOver, [param[1]]);

					break;
					
				case RoomMediator.CHAT_RESPONSE:
					_roomSence.writeChatText(param[0]);
					
					break;
					
				case RoomMediator.EXPRESSION_RESPONSE:
					var p:Point = _roomSence.playerSeats.getSeatPoint(param[0]);
					_roomSence.expressionManager.playExpression(p, param[2], param[1]);
					
					break;
					
				case RoomMediator.EXP_ADD_RESPONSE:
					user = param[0] as UserData;
				
					Log.debug("RoomMediator.EXP_ADD_RESPONSE", param[1]);
					
					_roomSence.toolbar.update(user, param[2], param[3], true);
					_roomSence.updateExp(user.seatid, param[1], param[2], param[3]);
				
					break;
				
				/**
				 * 表现层监听
				 */
				case LayerManagerMediator.GET_LAYERS:
					_roomSence.toolbar.layers = param[0];
					
					break;
					
				case SceneManagerMediator.CURRENT_SCENE:
					_roomSence.toolbar.sence = param[0];
					
					break;
			}
		}
		
		private function startDeal(selfSeatid:int, dice:String, startSeatid:int, seatsInPlay:Array, paisData:Array, clockTime:int, showPourSeatid:int):void
		{
			TweenLite.delayedCall(.3, _roomSence.actionDices, [dice]);//摇骰
			TweenLite.delayedCall(3, _roomSence.playerSeats.sendCard, [selfSeatid, startSeatid, seatsInPlay, paisData, clockTime, showPourSeatid]);//发牌
		}
		
		private function gameOver(count:int):void
		{
			_roomSence.showGameTip("over");
			
			_roomSence.chipsManager.clearAllChips();
			_roomSence.playerSeats.clearPaiList();
			_roomSence.playerSeats.resetSeats();
			
			TweenLite.delayedCall(1, showStartBtn, [count]);
		}
		
		private function showStartBtn(count:int):void
		{
			if (_selfSeatid == _bankerSeatid && count > 1)
			{
				_roomSence.playerSeats.setOperation(_selfSeatid, 'banker', 'startGame', true);
			}
		}
		
		private function playCallChipSound(chip:int):void
		{
			if (chip >= 1000 && chip < 10000)
			{
				sendNotification(SoundManagerMediator.PLAY, [SoundManagerMediator.MIDDLE_CHIP]);
			}else if (chip >= 10000)
			{
				sendNotification(SoundManagerMediator.PLAY, [SoundManagerMediator.BIG_CHIP]);
			}else
			{
				sendNotification(SoundManagerMediator.PLAY, [SoundManagerMediator.SMALL_CHIP]);
			}
		}
		
		public function setMask(oval:Sprite):void
		{
			_roomSence.assest.removeChild(oval);
			_roomSence.assest.mask = null;
		}
		
		public function get selfSeatid():int { return _selfSeatid; }
	}

}